package com.lzk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzk.constants.SystemConstants;
import com.lzk.domain.ResponseResult;
import com.lzk.domain.entity.Link;
import com.lzk.domain.vo.LinVo;
import com.lzk.domain.vo.PageVo;
import com.lzk.mapper.LinkMapper;
import com.lzk.service.LinkService;
import com.lzk.utils.BeanCopyUtils;
import io.jsonwebtoken.lang.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.Link_STATUS_NORMAL);
        List<Link> links = list(queryWrapper);
        //转化成vo
        List<LinVo> listVos = BeanCopyUtils.copyBeanList(links, LinVo.class);
        //封装返回
        return ResponseResult.okResult(listVos);
    }

    @Override
    public PageVo GetAllLink(Link link, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.hasText(link.getName()),Link::getName,link.getName());
        queryWrapper.like(Strings.hasText(link.getStatus()),Link::getStatus,link.getStatus());

        Page page = new Page();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());

        return pageVo;
    }
}
